//全屏
$(function(){
    $('.container').css('width',document.body.offsetWidth-200);
    $(document).delegate(".unfold", "click", function () {
        if ($(this).html() == "展开") {
            $(this).html('收起');
            $(this).prev()[0].style.whiteSpace = 'normal';
        }
        else {
            $(this).html('展开');
            $(this).prev()[0].style.whiteSpace = 'nowrap';
        }
    });
});
$(function(){
    $('#outLogin').live("click",function () {

        var isDel = window.confirm("确定要退出吗？");
        if (!isDel) {
            return;
        }
        getInterfaceInfo({
            url: interfaces.logout
        }, function (data) {
            if(data){
                setTimeout(function () {
                    window.location.href = "./index.html";
                }, 200);
            }
        });
        
    });
});

function getUrlKey(url){
    var str=window.location.search || window.location.hash;
    var arr=str.substring('1').split('&');
    var relust=null;
    var re = new RegExp(url+'=','g');
    for(var i= 0,len=arr.length;i<len;i++){
        if(re.test(arr[i])){
            relust=arr[i].split('=')[1];
        }
    }

    return relust;
}

function clearDoblue(arr){
    var json=[];
    var aRelust=[];

    for(var i=0;i<arr.length;i++)
    {
        if(!json[arr[i]])
        {
            aRelust.push(arr[i]);
            json[arr[i]]=1;
        }
    }

    return aRelust;
}

function upData(data){
    var newData=data.data;
    for(var i=0;i<newData.length;i++){
        newData[i].addTime = timeFormat(newData[i].addTime);
        newData[i].startTime = timeFormat(newData[i].startTime);
        newData[i].endTime = timeFormat(newData[i].endTime);
        if(newData[i].status){
            newData[i].status = s2Name(newData[i].status);
        }
    }
    return newData;
}


function toCenter(obj)
{
    obj.style.display='block';
    var h=parseInt(obj.offsetHeight/2,10);
    var w=parseInt(obj.offsetWidth/2,10);
    obj.style.marginTop=-h+'px';
    obj.style.marginLeft=-w+'px';

    if(obj.offsetTop<0){
        obj.style.marginTop=0+'px';
        obj.style.top=0+'px';
    }
};

function floterShow(name){
    var popBox= $(name);
    toCenter(popBox[0]);
    var close= popBox.find(".closePop");
    close.click(function(){
        popBox.hide();
    });
};

function toDoblue(num){
    return num>9?""+num:"0"+num;
};

function timeFormat(time){
    var date=new Date(time);
    var Y = date.getFullYear();
    var M = toDoblue(date.getMonth()+1);
    var day = toDoblue(date.getDate());
    var h = toDoblue(date.getHours());
    var m = toDoblue(date.getMinutes());
    var s = toDoblue(date.getSeconds());

    return Y+"/"+M+"/"+day+" "+h+":"+m+":"+s;
}

function getInterfaceInfo(param,callback){
    //param.data.from = opp_auth;
    $.ajax({
        type: param.type || 'GET',
        url: param.url,
        data: param.data,
        success: function(data){
            //data = data.data;
            callback && callback(data.data,data);
        },
        dataType: "jsonp",
        complete : function(a,b,c){
            /*if(a.status == 403){
                location.href = "./error-404.html";
            }*/
        }
    });
}

//获取翻页dom
$(function(){
    viewNum = $('#viewNum');
    allNumBox = $('#allNum');
    allPageBox = $('#allPage');
    curNumBox = $('#curNum');
});

//创建渲染构造函数
var CreateData= function(param,callback){
    this.init(param,callback);
};

CreateData.prototype.init = function(param,callback){
    this.vote_showList(param,callback);
};

CreateData.prototype.vote_showList = function(param,callback){
    var _this=this;
    getInterfaceInfo({
        type : "GET",
        url : param.url,
        data : param.msg
    },function(data){
        _this.readerData(param,data,callback);
    });
};

CreateData.prototype.readerData = function(param,data,callback){
    var box = param.parerBox;
    var tBody = param.tBody;
    data.count = data.total;
    param.allNum = Math.ceil(data.count / anyPage);
    var render = template.compile(param.template);
    if(data.count == 0){
        var html = "<h3>没有相关数据</h3>";
        tBody.html(html);
    }else{
        var html = param.tbodyTitle + render(data);
        tBody.html(html);
    }
    

    
    if (!param.allNum) {
        box.hide();
        callback && callback();
        return;
    }else{
        box.show();
    }
    allpage = param.allNum;
    allNum = data.total;
    curNum = param.curNum;
    viewNum.val(anyPage);
    allNumBox.html(allNum + ' 条');
    allPageBox.html(allpage + ' 页');
    curNumBox.val(curNum);
    callback && callback();
    document.body.scrollTop = 0;
    document.documentElement
        .scrollTop = 0;
      
};

CreateData.prototype.pager = function(param,callback){
    //移除分页所有事件
    $('#firstPage,#pager_prev ,#pager_next, #lastPage, #viewNum, #curNum, body').unbind();

    var _this=this;
    __pager_prev = function(){
        param.curNum--;
        if (param.curNum <= 0) {
            param.curNum = 1;
            return;
        }
        param.msg.page = param.curNum;
        _this.vote_showList(param, callback);
    };

    __pager_next = function(){
        param.curNum++;
        if (param.curNum >= param.allNum + 1) {
            param.curNum = param.allNum;
            return;
        }
        param.msg.page = param.curNum;
        _this.vote_showList(param, callback);
    };

    //首页
    $('#firstPage').on('click', function () {
        param.msg.page = 1;
        _this.vote_showList(param, callback);
    });

    //上一页
    $('#pager_prev').on('click', function () {
        __pager_prev();
    });

    //下一页
    $('#pager_next').on('click', function () {
        __pager_next();
    });

    //尾页
    $('#lastPage').on('click', function () {
        param.msg.page = allpage;
        _this.vote_showList(param, callback);
    });

    $('#viewNum').keydown(function(ev){
        if(ev.keyCode == 13){
            anyPage = $(this).val();
            param.msg.size = anyPage;
            _this.vote_showList(param, callback);
        }
    });

    $('#curNum').keydown(function(ev){
        if (ev.keyCode == 13) {
            param.msg.page = $(this).val();
            _this.vote_showList(param);
        }
    });

    $('body').keydown(function (ev) {
        switch(ev.keyCode){
            case 37 :
                __pager_prev();
                break;
            case 39 :
                __pager_next();
                break;
        }
    });
};

CreateData.prototype.addEvent = function(param,fn){
    fn.call(this,param);
};




